package de.tum.team26.eistpoll.client.UI;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.team26.eistpoll.client.EIST_POLL;

/**
 * Class to show the Login Panel
 */
public class Login extends UIElement {

	// UI Elements
	VerticalPanel loginPanel = new VerticalPanel(); // Login Container
	VerticalPanel loginPanelTop = new VerticalPanel();
	VerticalPanel loginPanelHeadder = new VerticalPanel();
	Button btnSendLogin = new Button("Login"); // Submitt Button for Poll
	TextBox txtUser = new TextBox();
	PasswordTextBox txtPass = new PasswordTextBox();
	Label headding = new Label("Lecturer Login:");
	Label lblUser = new Label("User Name:");
	Label lblPass = new Label("Password:");
	Label lblErrMsg;

	// Controller
	EIST_POLL controller;
	
	/**
	 * Initialise Panel
	 */
	public Login(EIST_POLL controller) {
		this.controller=controller;
		headding.setStyleName("h1");
		btnSendLogin.addStyleDependentName("submit");
		loginPanelHeadder.setStyleName("headder");
		loginPanel.add(loginPanelHeadder);
		loginPanel.add(headding);
		loginPanelTop.add(lblUser);
		loginPanelTop.add(txtUser);
		loginPanelTop.add(lblPass);
		loginPanelTop.add(txtPass);
		loginPanelTop.setStyleName("loginInput");
		loginPanel.add(loginPanelTop);
		loginPanel.add(btnSendLogin);
		loginPanel.addStyleName("container");
		
		// add click Handler
		btnSendLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tryLogin();
			}
		});
	}
	
	// As always:
	
	@Override
	public VerticalPanel getContainer() {
		return loginPanel;
	}
	
	private void tryLogin(){
		controller.tryLogin(this, txtUser.getText(), txtPass.getText());
	}

	@Override
	public void displayErrorMessage(String message) {
		if (lblErrMsg == null){
			lblErrMsg = new Label(message);
			lblErrMsg.setStyleName("error");
			loginPanel.add(lblErrMsg);
		}
		else{
			lblErrMsg.setText(message);
		}
	}
}
